package email

import (
	"fmt"
	"strings"
)

// 邮件信息
type MailInfo struct {
	Subject string
	Body    string
	To      []string
	Cc      []string
	From    string
}

type SmtpConfig struct {
	Id          string `mapstructure:"id"`           // 邮件发送者
	Host        string `mapstructure:"host"`         // 邮件服务器地址
	Port        string `mapstructure:"port"`         // 邮件服务器端口
	Username    string `mapstructure:"user_name"`    // 邮箱地址
	Password    string `mapstructure:"password"`     // 邮箱密码
	Expire      int    `mapstructure:"expire"`       // 过期时间分钟
	TitlePrefix string `mapstructure:"title_prefix"` // 邮件标题前缀
	FromPrefix  string `mapstructure:"from_prefix"`  // 发件人前缀

}

func (info MailInfo) GetMessage() string {
	header := make(map[string]string)
	header["From"] = info.From
	header["To"] = strings.Join(info.To, ",")
	if len(info.Cc) > 0 {
		header["Cc"] = strings.Join(info.Cc, ",")
	}

	header["Subject"] = info.Subject
	header["Content-Type"] = "text/html; charset=UTF-8"

	body := info.Body
	message := ""
	for k, v := range header {
		message += fmt.Sprintf("%s: %s\r\n", k, v)
	}
	message += "\r\n" + body
	return message
}
